/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.edittaxon;

import java.io.File;
import java.io.IOException;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import hu.tetete.hibiscus.R;
import hu.tetete.hibiscus.model.Property;
import hu.tetete.hibiscus.model.Taxon;
import hu.tetete.hibiscus.propertytree.PropertyTreeListAdapter;
import hu.tetete.hibiscus.taxondetails.TaxonDetailsTabActivity;

/**
 * TODO javadoc.
 */
public class EditTaxonPropertyTreeActivity extends EditTaxonAbstractActivity {

    /**
     * Intent parameter.
     */
    public enum INTENT_EXTRA {
        /**
         * A property object.
         */
        PROPERTY
    }

    /**
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        final LayoutInflater inflater    = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ViewGroup     contentView = (ViewGroup)inflater.inflate(R.layout.edit_taxon_properties, null);
        final ImageView     nextButton  = (ImageView)contentView.findViewById(R.id.nextButton);
        final TextView      taxonName   = (TextView)contentView.findViewById(R.id.taxonName);
        final TextView      scienceName = (TextView)contentView.findViewById(R.id.scienceName);
        final ImageView     taxonImage  = (ImageView)contentView.findViewById(R.id.taxonImage);
        super.onCreate(savedInstanceState, new ServiceConnection() {

            @Override
            public void onServiceDisconnected(final ComponentName name) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onServiceConnected(final ComponentName name, final IBinder service) {
                final Intent incomeIntent = getIntent();
                final Property<?> property = incomeIntent == null || incomeIntent.getExtras() == null ? null : (Property<?>)incomeIntent.getExtras().get(INTENT_EXTRA.PROPERTY.name());

                final ListView listView = new ListView(EditTaxonPropertyTreeActivity.this);

//                final NewTaxonService newTaxonService = ((NewTaxonServiceBinder)service).getService();
                final ListAdapter adapter = new PropertyTreeListAdapter(EditTaxonPropertyTreeActivity.this,
                        EditTaxonPropertyTreeActivity.this.editTaxonService.getPropertyDao().getPropertyChildrenById(property == null ? null : property.getId()),
                        EditTaxonPropertyTreeActivity.this.editTaxonService, EditTaxonPropertyTreeActivity.class);

                listView.setAdapter(adapter);

                nextButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(final View v) {
                        Toast.makeText(EditTaxonPropertyTreeActivity.this, "Taxon is being saved", Toast.LENGTH_LONG).show();
                        //TODO teaser!
                        try {
                            final Taxon  newTaxon = EditTaxonPropertyTreeActivity.this.editTaxonService.save();
                            final Intent newTaxonIntent = new Intent(EditTaxonPropertyTreeActivity.this, TaxonDetailsTabActivity.class);
                            newTaxonIntent.putExtra(TaxonDetailsTabActivity.INTENT_EXTRA.TAXON.name(), newTaxon);
                            startActivity(newTaxonIntent);
                            //TODO everything else is ok. So we should navigate if the photo is not saved.
                        } catch (final IOException e) {
                            //TODO teaser we can't save the image.
                            Log.e(TAG, "The photo can't be saved", e);
                        }
                    }
                });


                final File taxonPhoto = editTaxonService.getPhoto();
                if (taxonPhoto != null) {
                    taxonImage.setImageURI(Uri.fromFile(taxonPhoto));
                }
                taxonName.setText(editTaxonService.getName());
                scienceName.setText(editTaxonService.getScienceName());
                nextButton.setImageResource(android.R.drawable.ic_menu_save);
                contentView.addView(listView);

            }
        });

        setContentView(contentView);


    }


}
